package com.busibuilding.administrate.area.action;

import java.util.List;

import org.displaytag.tags.TableTagParameters;

import com.busibuilding.administrate.area.service.AreaService;
import com.busibuilding.administrate.area.vo.AreaCVO;
import com.busibuilding.common.action.CommonActionImpl;
import com.busibuilding.utils.StringUtil;
/**
 * Class Name: AreaAction.java
 * Description: 
 * @author Lin Yanjun
 * @version 1.0
 * @since 1.0
 * Date: 2010-2-25
 */
public class AreaAction extends CommonActionImpl{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2356838044617244776L;
	
	private AreaForm areaForm = new AreaForm();
	
	private boolean showAdd;
	private boolean showUpdate;
	private boolean showTable;
	
	private int page;
	private int currentPage;
	
	private AreaService service;

	public String search() throws Exception{
		AreaCVO areaCVO = new AreaCVO();
		if(StringUtil.isNotEmpty(areaForm.getCityCd())){
			areaCVO.setCityCd(areaForm.getCityCd());
		}else{
			areaCVO.setCityCd(areaForm.getCityCd());
		}
		if(StringUtil.isNotEmpty(areaForm.getAreaCd())){
			areaCVO.setAreaCd(areaForm.getAreaCd());
		}
		if(StringUtil.isNotEmpty(areaForm.getAreaName())){
			areaCVO.setAreaName(areaForm.getAreaName());
		}
		if(StringUtil.isNotEmpty(areaForm.getDefunctInd())){
			areaCVO.setDefunctInd(areaForm.getDefunctInd());
		}else{
			areaCVO.setDefunctInd("N");
		}
		areaCVO.setMinIndex((getPage()-1)*10);
		areaCVO.setMaxIndex(getPage()*10);
		int listSize = service.getRecordsCount(areaCVO);
		List<AreaCVO> resultList = service.searchArea(areaCVO);
		this.getRequest().setAttribute("areaListSize", listSize);
		this.getRequest().setAttribute("areaDisplayList", resultList);
		areaForm.setCityCd(areaCVO.getCityCd());
		showTable = true;
		return INPUT;
	}
	
	public String del() throws Exception{
		AreaCVO areaCVO = new AreaCVO();
		areaCVO.setAreaId(areaForm.getAreaId());
		areaCVO.setDefunctInd(YES);
		getAdminCommonInformationForUpdate(areaCVO);
		int code = service.updateArea(areaCVO);
		if(code==SQLMAP_COMMON_RESULT_ERROR){
			txnFailure(TRANS_SUCCESSFUL_MSG);
		}else{
			txnSuccessful(TRANS_SUCCESSFUL_MSG);
		}
		return "toSearchArea";
	}
	
	public String inputCreate() throws Exception{
		showAdd = true;
		showUpdate = false;
		showTable = false;
		return INPUT;
	}
	
	public String createArea() throws Exception{
		if (hasErrors()) {
			return INPUT;
		}
		AreaCVO areaCVO = new AreaCVO();
		areaCVO.setAreaCd(areaForm.getAreaCd());
		List<AreaCVO> resultList = service.searchArea(areaCVO);
		if(resultList.size()>0){
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("areaForm.areaCd", getText(
					"error.admin.sysmgmt.area.existAreaCode",
					new String[] { areaForm.getAreaCd() }));
			areaForm.setAreaCd(EMPUT_STRING);
			return INPUT;
		}
		areaCVO.setAreaName(areaForm.getAreaName());
		areaCVO.setCityCd(areaForm.getCityCd());
		getAdminCommonInformationForCreate(areaCVO);
		service.createArea(areaCVO);
		
		return "toSearchArea";
	}
	
	public String inputUpdate() throws Exception{
		AreaCVO areaCVO = new AreaCVO();
		areaCVO.setAreaId(areaForm.getAreaId());
		areaCVO = (AreaCVO) service.getArea(areaCVO);
		areaForm.setAreaCd(areaCVO.getAreaCd());
		areaForm.setAreaName(areaCVO.getAreaName());
		areaForm.setCityCd(areaCVO.getCityCd());
		showAdd = false;
		showUpdate = true;
		showTable = false;
		return INPUT;
	}
	
	public String updateArea() throws Exception{
		if (hasErrors()) {
			return INPUT;
		}
		AreaCVO tempAreaCVO = new AreaCVO();
		tempAreaCVO.setAreaCd(areaForm.getAreaCd());
		tempAreaCVO.setCityCd(areaForm.getCityCd());
		List<AreaCVO> resultList = service.searchArea(tempAreaCVO);
		if(resultList.size()>1){
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("areaForm.areaCd", getText(
					"error.admin.sysmgmt.area.existAreaCode",
					new String[] { areaForm.getCityCd() }));
			areaForm.setAreaCd(EMPUT_STRING);
			return INPUT;
		}
		AreaCVO areaCVO = new AreaCVO();
		areaCVO.setAreaId(areaForm.getAreaId());
		areaCVO = (AreaCVO) service.getArea(areaCVO);
		areaCVO.setAreaCd(areaForm.getAreaCd());
		areaCVO.setAreaName(areaForm.getAreaName());
		getAdminCommonInformationForUpdate(areaCVO);
		areaCVO.setVersion(areaCVO.getVersion()+1);
		service.updateArea(areaCVO);
		return "toSearchCity";
	}

	public AreaForm getAreaForm() {
		return areaForm;
	}

	public void setAreaForm(AreaForm areaForm) {
		this.areaForm = areaForm;
	}

	public boolean isShowAdd() {
		return showAdd;
	}

	public void setShowAdd(boolean showAdd) {
		this.showAdd = showAdd;
	}

	public boolean isShowUpdate() {
		return showUpdate;
	}

	public void setShowUpdate(boolean showUpdate) {
		this.showUpdate = showUpdate;
	}

	public boolean isShowTable() {
		return showTable;
	}

	public void setShowTable(boolean showTable) {
		this.showTable = showTable;
	}

	public int getPage() {
		if (getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE) == null || "".equals(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE))) {
        	page = currentPage;
        }else{
        	page = Integer.parseInt(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE));
        }
		
		if (page == 0){
			page = DEFAULT_PAGE;
		}
		
		return page;
	}

	public void setPage(int page) {
		if (page > 0){
			this.page = page;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setService(AreaService service) {
		this.service = service;
	}
	
}
